package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.ClaimShopParams;
import com.huizhonglm.diap.api.model.user.ClaimShopResponse;
import com.huizhonglm.diap.core.storage.model.ClaimShopInfo;
import com.huizhonglm.diap.presenters.BasePresenter;
import com.huizhonglm.diap.ui.user.iView.ClaimShopIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-03-07 13:53
 */
public class ClaimShopPresenter extends BasePresenter {

    private ClaimShopIView iView;

    public ClaimShopPresenter(ClaimShopIView iView) {
        this.iView = iView;
    }


    public void getClaimShop(String userId) {
        ClaimShopParams params = new ClaimShopParams(API.buildUri(API.CLAIM_SHOP_GET));
        params.userId = userId;
        API.http().post("getClaimShop", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ClaimShopResponse resp = JSON.parseObject(result, ClaimShopResponse.class);
                    if (resp != null) {
                        ClaimShopInfo info = new ClaimShopInfo();
                        info.setShopId(resp.getShopId());
                        info.setShopName(resp.getShopName());
                        info.setShopLogo(resp.getShopLogo());
                        info.setShopDesc(resp.getShopDesc());
                        iView.onClaimShopSuccess(info);
                    } else {
                        iView.onClaimShopFailure();
                    }
                } else {
                    iView.onClaimShopFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onClaimShopFailure();
            }
        });
    }

    public void claimShop(String userId, final String shopId) {
        ClaimShopParams params = new ClaimShopParams(API.buildUri(API.CLAIM_SHOP));
        params.userId = userId;
        params.shopId = shopId;
        API.http().post("claimShop", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    ClaimShopResponse resp = JSON.parseObject(result, ClaimShopResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())) {
                        ClaimShopInfo info = new ClaimShopInfo();
                        info.setShopId(shopId);
                        info.setShopName(resp.getShopName());
                        info.setShopLogo(resp.getShopLogo());
                        info.setShopDesc(resp.getShopDesc());
                        iView.onClaimShopSuccess(info);
                    } else {
                        iView.onClaimShopFailure();
                    }
                } else {
                    iView.onClaimShopFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onClaimShopFailure();
            }
        });
    }
}
